//
//  TWScrollLabelView.m
//  特效动画
//
//  Created by 耐克了解了 on 1/6/17.
//  Copyright © 2017年 耐克了解了. All rights reserved.
//

#import "TWScrollLabelView.h"

@interface TWScrollLabelView ()<CAAnimationDelegate>{
    
    CGFloat _width;
    CGFloat _height;
    
    CGFloat _animationViewWidth;
    CGFloat _animationViewHeight;
    
    BOOL _stoped;
    UIView * _contentView;
    
}

@property (nonatomic,strong) UIView *animationView;

@end

@implementation TWScrollLabelView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _width = frame.size.width;
        _height = frame.size.height;
        
        self.speed = 1.0f;
        self.barrageDirection = FromLeftType;
        self.layer.masksToBounds = YES;
        self.animationView = [[UIView alloc]initWithFrame:CGRectMake(_width, 0, _width, _height)];
        [self addSubview:self.animationView];
        
    }
    return  self;
}

- (void)addContentView:(UIView *)view
{
    [_contentView removeFromSuperview];
    
    view.frame = view.bounds;
    _contentView = view;
    self.animationView.frame = view.bounds;
    [self.animationView addSubview:_contentView];
    _animationViewWidth = self.animationView.frame.size.width;
    _animationViewHeight = self.animationView.frame.size.height;
}

- (void)startAnimation
{
    [self.animationView.layer removeAnimationForKey:@"animationViewPosition"];
    _stoped = NO;
    CGPoint pointRightCenter = CGPointMake(_width + _animationViewWidth / 2.0f, _animationViewHeight / 2.0f);
    CGPoint pointLeftCenter = CGPointMake(-_animationViewWidth / 2, _animationViewHeight / 2.0f);
    CGPoint fromPoint = self.barrageDirection == FromLeftType ? pointRightCenter : pointLeftCenter;
    CGPoint toPoint = self.barrageDirection == FromLeftType? pointLeftCenter : pointRightCenter;
    self.animationView.center = fromPoint;
    
    UIBezierPath *movePath = [UIBezierPath bezierPath];
    [movePath moveToPoint:fromPoint];
    [movePath addLineToPoint:toPoint];
    
    CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    moveAnimation.path = movePath.CGPath;
    moveAnimation.removedOnCompletion = YES;
    moveAnimation.duration = _animationViewWidth / 30.0f * (1 /self.speed);
    moveAnimation.delegate = self;
    [self.animationView.layer addAnimation:moveAnimation forKey:@"animationViewPosition"];
    
}


- (void)stopAnimation
{
    _stoped = YES;
    [self.animationView.layer removeAnimationForKey:@"animationViewPosition"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(barrageView:animationDidStopFinished:)]) {
        [self.delegate barrageView:self animationDidStopFinished:flag];
    
    }
    
    if (flag && !_stoped) {
        [self startAnimation];
    }
}

- (void)pauseAnimation
{
    [self pauselayer:self.animationView.layer];
}

- (void)resumeAnimation
{
    [self resumeLayer:self.animationView.layer];
}

- (void)pauselayer:(CALayer *)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() toLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

- (void)resumeLayer: (CALayer*)layer
{
    CFTimeInterval pausedTime = layer.timeOffset;
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() toLayer:nil]- pausedTime;
    layer.beginTime = timeSincePause;
}






























@end
